import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/about',
    name: 'about',
    component: () => import('../views/AboutView.vue')
  },
  {
    path:'/user',
    component: () => import('../views/UserView.vue'),
    children:[
      {
        path:'login', // 相对路径 /user/login
        component: () => import('../views/LoginView.vue'),
        name:'login'
      },
      {
        path:'register/:name/:age/:sex', 
        component: () => import('../views/RegisterView.vue'),
        name:'register',
        meta:{
          flag:false,
          title:'注册'
        }
      }
    ]
  },
  {
    path:'*',
    redirect: '/' // 重定向到首页
  }
]

const router = new VueRouter({
  routes,
  mode:'history' // 修改路由的模式，改为history模式，去掉url中的#
})

// 全局路由守卫
// router.beforeEach((to, from, next) => {
  // console.log(to)
  // // 如果访问首页/登录页/注册页，放行
  // if(to.path === '/' || to.path === '/user/login' || to.name === 'register'){
  //   return next()
  // }

  // // 如果访问其他页面，判断是否登录
  // if(localStorage.getItem('token')){
  //   return next()
  // }

  // 如果没有登录，跳转到登录页
  // next('/user/login')

// })

export default router
